{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tvos 13\n",
      "tvos 12\n",
      "tvos 11\n",
      "tvos 10\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# Read the JSON files into an array so we can iterate over them\n",
    "\n",
    "tvOsReleaseJSON = []\n",
    "\n",
    "min_os = 10\n",
    "max_os = 13\n",
    "\n",
    "for os_number in range(max_os, min_os-1,-1):\n",
    "    print('tvos',os_number)\n",
    "    \n",
    "    with open('./resources/tvos'+str(os_number)+'.json') as json_file:\n",
    "        dataDict = json.load(json_file)\n",
    "        tvOsReleaseJSON.append(dataDict);\n",
    "    \n",
    "# pandas.read_json('./resources')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup some functions to parse the JSON that we read in previously.\n",
    "\n",
    "\n",
    "# `url-1080-H264` (1080p H.264, most compatible format), \n",
    "# `url-1080-SDR`  (1080p HEVC, better quality, requires a recent Mac for hardware decoding)\n",
    "# `url-4K-SDR`    (4K HEVC)\n",
    "\n",
    "# HDR versions of the above will only play in Catalina (recent/fast Mac recommended)\n",
    "\n",
    "\n",
    "# ['url', 'accessibilityLabel', 'type', 'id', 'timeOfDay', 'url-1080-SDR', 'url-1080-HDR', 'url-4K-SDR', 'url-4K-HDR', 'pointsOfInterest', 'url-1080-H264']\n",
    "available_keys = []\n",
    "# Gets the list of keys from each asset objects.\n",
    "# Helpful for finding the list of available keys - i.e features available \n",
    "def parse_keys_for_asset_list(assets):\n",
    "    for asset in assets:\n",
    "        for key in asset.keys():\n",
    "            if (not key in available_keys):\n",
    "                available_keys.append(key)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# Each asset object in the assets list has some of the following attributes (at least):\n",
    "# [\"id\", \"url-1080-SDR\", \"url-1080-HDR\", \"url-4K-SDR\", \"url-4K-HDR\", \"accessibilityLabel\"] \n",
    "# accessibilityLabel is the readable one - i.e a decent title.\n",
    "def _parse_asset_list(assets):\n",
    "#     print(type(assets)) \n",
    "    # Assets is always an array of objects, (found when first run)\n",
    "\n",
    "    parse_keys_for_asset_list(assets)\n",
    "    uniform_mode = False; #set this to True to only output url, accesibilityLabel and URLs.\n",
    "    \n",
    "    for asset in assets:\n",
    "        if not uniform_mode:\n",
    "            parsed_assets[asset['id']]= asset\n",
    "            \n",
    "        else:\n",
    "            new_asset = {}\n",
    "\n",
    "            new_asset['id'] = asset['id']\n",
    "            new_asset['accessibilityLabel'] = asset['accessibilityLabel']\n",
    "\n",
    "            if \"url\" in asset:\n",
    "                new_asset['url'] = asset['url']\n",
    "\n",
    "            if \"url-1080-SDR\" in asset:\n",
    "                new_asset['url-1080-SDR'] = asset['url-1080-SDR']\n",
    "\n",
    "            if \"url-1080-HDR\" in asset:\n",
    "                    new_asset[\"url-1080-HDR\"] = asset[\"url-1080-HDR\"]\n",
    "\n",
    "            if \"url-4K-SDR\" in asset:\n",
    "                    new_asset[\"url-4K-SDR\"] = asset[\"url-4K-SDR\"]\n",
    "\n",
    "            if \"url-4K-HDR\" in asset:\n",
    "                    new_asset[\"url-4K-HDR\"] = asset[\"url-4K-HDR\"]\n",
    "\n",
    "            if \"url-1080-H264\" in asset:\n",
    "                    new_asset[\"url-1080-H264\"] = asset[\"url-1080-H264\"]\n",
    "            parsed_assets[asset['id']]= new_asset # Using an object ensures no duplicates\n",
    "                \n",
    "\n",
    "\n",
    "    \n",
    "def iterate_parse_list_or_array(iterable):\n",
    "    \n",
    "    if isinstance(iterable,list):\n",
    "        for value in iterable:\n",
    "            iterate_parse_list_or_array(value)\n",
    "\n",
    "    \n",
    "    # Iterate over keys if it's a dict\n",
    "    if isinstance(iterable, dict):\n",
    "        for key, value in iterable.items():\n",
    "            if(key == \"assets\"):\n",
    "                _parse_asset_list(value)\n",
    "            else:\n",
    "                iterate_parse_list_or_array(value)\n",
    "       \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "succesfully output_parsed_file_list.json\n"
     ]
    }
   ],
   "source": [
    "# Iterate over the list of JSON files and parse each one. Each one should somewhere in it have a list of assets. \n",
    "# We just search through the keys until we find that list of assets and then we can parse it.\n",
    "\n",
    "parsed_assets = {}\n",
    "for tvos in tvOsReleaseJSON:\n",
    "    iterate_parse_list_or_array(tvos)\n",
    "    \n",
    "    \n",
    "# print(available_keys)\n",
    "parsed_assets = list(parsed_assets.values())\n",
    "# print(json.dumps(parsed_assets, indent=4))\n",
    "\n",
    "with open('output_parsed_file_list.json', 'w', encoding='utf-8') as f:\n",
    "    json.dump(parsed_assets, f, ensure_ascii=False, indent=4)\n",
    "    print(\"succesfully output_parsed_file_list.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess\n",
    "\n",
    "# Requires FFMPEG\n",
    "# https://stackoverflow.com/questions/27568254/how-to-extract-1-screenshot-for-a-video-with-ffmpeg-at-a-given-time\n",
    "def get_thumbnail(vid_id, url):\n",
    "    print(url);\n",
    "    subprocess.call(['ffmpeg', \"-ss\" ,\"00:00:00\" ,'-i', url, \"-vframes\", \"1\", '-q:v', '1', \"thumbnails/\"+vid_id+\".jpeg\"])\n",
    "\n",
    "    \n",
    "# Enable this to grab and output thumbnails (to the thumbnails directory) but takes a while\n",
    "generate_thumbnails = False;\n",
    "\n",
    "if generate_thumbnails:\n",
    "    for asset in parsed_assets:\n",
    "        url = \"\"\n",
    "        if \"url\" in asset:\n",
    "            url = asset['url']\n",
    "            \n",
    "        elif \"url-1080-SDR\" in asset:\n",
    "            url = asset[\"url-1080-SDR\"]\n",
    "\n",
    "        elif \"url-1080-H264\" in asset:\n",
    "            url = asset[\"url-1080-H264\"]\n",
    "\n",
    "        elif \"url-1080-HDR\" in asset:\n",
    "            url = asset[\"url-1080-HDR\"]\n",
    "\n",
    "        elif \"url-4K-SDR\" in asset:\n",
    "            url = asset[\"url-4K-SDR\"]\n",
    "\n",
    "        elif \"url-4K-HDR\" in asset:\n",
    "            url = asset[\"url-4K-HDR\"]\n",
    "        \n",
    "        if(len(url) > 0):\n",
    "            get_thumbnail(asset['id'], url)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finished writing to md file: list_of_files_and_titles.md\n"
     ]
    }
   ],
   "source": [
    "markdown_output = []\n",
    "\n",
    "markdown_output.append(\"# Aerial wallpapers   \")\n",
    "markdown_output.append(\"Amazing footage from AppleTV default wallpapers. Mostly drone footage of various cities. Features some satellite footage and underwater footage too! Built from https://github.com/Tawfiqh/aerialWallpapers  \")\n",
    "\n",
    "markdown_output.append('<div class=\"asset_container\" markdown=\"1\">  ')\n",
    "markdown_output.append(\"  \")\n",
    "\n",
    "for asset in parsed_assets:\n",
    "    markdown_output.append('<div class=\"asset\" markdown=\"1\">  ')\n",
    "    markdown_output.append(\"  \")\n",
    "    \n",
    "    new_line = \"## \" + asset['accessibilityLabel'] + \" _(\"+ asset['id'] +\")_\"\n",
    "    markdown_output.append(new_line)\n",
    "    \n",
    "    # ![Alt text](/path/to/img.jpg \"Optional title\")\n",
    "    # images in markdown.\n",
    "    new_line = \"![{}]({} '{}')   \".format(asset['accessibilityLabel'], \"thumbnails/\"+asset['id']+\".jpeg\", asset['accessibilityLabel'])\n",
    "    markdown_output.append(new_line)\n",
    "    \n",
    "    markdown_output.append('<div class=\"button_container\" markdown=\"1\">  ')\n",
    "    markdown_output.append(\"  \")\n",
    "\n",
    "    if \"url\" in asset:\n",
    "        new_line = \"[🎬 Watch ](\" + asset['url'] +\")   \"\n",
    "        markdown_output.append(new_line)\n",
    "\n",
    "    if \"url-1080-SDR\" in asset:\n",
    "        new_line = \"[🎬 Watch 1080p](\" + asset['url-1080-SDR'] +\")   \"\n",
    "        markdown_output.append(new_line)\n",
    "\n",
    "\n",
    "    if \"url-4K-SDR\" in asset:\n",
    "        new_line = \"[🎬 Watch *4K* ](\" + asset['url-4K-SDR'] +\")   \"\n",
    "        markdown_output.append(new_line)\n",
    "\n",
    "    if \"url-1080-H264\" in asset:\n",
    "        new_line = \"[🎬 Watch 1080p-H264 ](\" + asset['url-1080-H264'] +\")   \"\n",
    "        markdown_output.append(new_line)\n",
    "\n",
    "\n",
    "    if \"url-1080-HDR\" in asset or \"url-4K-HDR\" in asset:\n",
    "        markdown_output.append(\" \")\n",
    "        markdown_output.append(\"#### HDR\")\n",
    "        if \"url-1080-HDR\" in asset:\n",
    "            new_line = \"[🎬 Watch 1080-HDR ](\" + asset['url-1080-HDR'] +\")   \"\n",
    "            markdown_output.append(new_line)\n",
    "\n",
    "\n",
    "        if \"url-4K-HDR\" in asset:\n",
    "            new_line = \"[🎬 Watch 4K-HDR ](\" + asset['url-4K-HDR'] +\")   \"\n",
    "            markdown_output.append(new_line)\n",
    "    markdown_output.append(\"</div>  \")\n",
    "\n",
    "    markdown_output.append(\"  \")\n",
    "    markdown_output.append(\"</div>  \")\n",
    "    markdown_output.append(\"   \");    markdown_output.append(\"   \")\n",
    "\n",
    "markdown_output.append(\"  \")\n",
    "markdown_output.append(\"</div>   \")\n",
    "    \n",
    "output_markdown_file = \"list_of_files_and_titles.md\"\n",
    "with open(output_markdown_file, 'w') as f:\n",
    "    f.writelines(\"%s\\n\" % l for l in markdown_output)\n",
    "    print(\"Finished writing to md file:\",output_markdown_file)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
